package db_live

import (
	"fmt"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_live"

	"gorm.io/gorm"
)

func GetLiveRoomBanneduserParam(param map[string]interface{}) (*models_live.LiveRoomBanneduser, error) {
	fName := "GetLiveRoomBanneduserParam"
	o := &models_live.LiveRoomBanneduser{}
	table := global.Live().Model(&models_live.LiveRoomBanneduser{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func InsertLiveRoomBanneduser(bannedUser *models_live.LiveRoomBanneduser) error {
	fName := "InsertLiveRoomBanneduser"
	err := global.Live().Create(bannedUser).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func DeleteLiveRoomBanneduser(bannedUser *models_live.LiveRoomBanneduser) error {
	fName := "DelLiveRoomBanneduser"
	err := global.Live().Where("id = ?", bannedUser.Id).Delete(bannedUser).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

//param:要更新的列
func UpdateLiveRoomBanneduser(bannedUser *models_live.LiveRoomBanneduser, param []string) error {
	fName := "UpdateLiveRoomBanneduser"
	err := global.Live().Select(param).Updates(bannedUser).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func GetLiveRoomBannedusersParam(param map[string]interface{}) ([]*models_live.LiveRoomBanneduser, error) {
	fName := "GetLiveRoomBannedusersParam"
	o := []*models_live.LiveRoomBanneduser{}
	table := global.Live().Model(&models_live.LiveRoomBanneduser{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
